<!-- 登录后右上角切换仓库组件 -->
<template>
  <el-dropdown trigger="click" @command="handleSetWarehouse">
    <span class="el-dropdown-link">
      {{ warehouseName }}<i class="el-icon-arrow-down el-icon--right" />
    </span>
    <el-dropdown-menu slot="dropdown">
      <div id="el-dropdown-menu">
        <el-dropdown-item
          class="el-dropdown-item"
          v-for="item in warehouseList"
          :key="item.code"
          :disabled="authorizeDataCode === item.code"
          :command="item.code"
        >
          {{ item.name }}
        </el-dropdown-item>
      </div>
    </el-dropdown-menu>
  </el-dropdown>

</template>

<script>
import request from '@/utils/request'

export default {
  computed: {
    authorizeDataCode() {
      return this.$store.getters.authorizeDataCode
    },
    warehouseName() {
      return this.$store.getters.warehouse_name
    },
    warehouseList() {
      return this.$store.getters.warehouse_list
    }
  },
  methods: {
    handleSetWarehouse(warehouseCode) {
      const currentData = this.warehouseList.filter(item => item.code === warehouseCode)
      const { code, name } = currentData[0]
      // this.$store.commit('user/SET_CODE', createdBy)
      this.$store.commit('user/SET_WAREHOUSE_NAME', name)
      this.$store.commit('user/SET_AUTHORIZEDATACODE', code)
      request({
        url: '/arpa-basic-api/changeLogin',
        method: 'post',
        params: {
          dataType: this.$store.getters.dataType || 'WAREHOUSE',
          authorizeDataCode: code
        }
      }).then(response => {
        this.$store.dispatch('tagsView/delAllViews', null, { root: true })
        if (this.$route.path === '/home') {
          this.$eventBus.$emit('refreshLineChart')
        } else {
          this.$router.push({ path: '/' })
        }
      })
    }
  }
}
</script>
<style>
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
    font-size: 15px;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
   #el-dropdown-menu{
    max-height:300px;
    overflow: auto;
  }
  .el-dropdown-item{
    font-size: 14px;
    padding: 0 20px;
    position: relative;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    height: 34px;
    line-height: 34px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    cursor: pointer
  }
  ::-webkit-scrollbar-track-piece {
    -webkit-border-radius: 0
  }
  ::-webkit-scrollbar {
    width: 5px;
    height: 10px
  }
  ::-webkit-scrollbar-thumb {
    height: 50px;
    background-color: #b8b8b8;
    -webkit-border-radius: 6px;
    outline-offset: -2px;
    filter: alpha(opacity = 50);
    -moz-opacity: 0.5;
    -khtml-opacity: 0.5;
    opacity: 0.5
  }
  ::-webkit-scrollbar-thumb:hover {
    height: 50px;
    background-color: #878987;
    -webkit-border-radius: 6px
  }
</style>
